//
//  SSCAdsUnityAdapter.m
//  Unity-iPhone
//
//  Created by xuzepei on 16/4/13.
//
//

#import "SSCNewsBlastAdapter.h"
#if __has_include(<SSCAdsSDK/SSCAdsSDK.h>)
#import <SSCAdsSDK/SSCAdsSDK.h>
#else
#import "AdIdHelper.h"
#endif


@implementation SSCNewsBlastAdapter

+ (SSCNewsBlastAdapter*)getInstance
{
    static SSCNewsBlastAdapter* sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token, ^{
        sharedInstance = [[SSCNewsBlastAdapter alloc] init];
    });
    
    return sharedInstance;
}

- (void)setGameObjectName:(char*)name
{
    if(name && strlen(name))
    {
        self.objectName = [NSString stringWithFormat:@"%s",name];
    }
}

- (void)startNews:(bool)isDebugMode
{
    NewsBlast* newsblast = (NewsBlast*)[NewsBlast shareNews];
    newsblast.delegate = self;
    [[NewsBlast shareNews] sendNews:isDebugMode];
}

- (void)cancelShowNewsBlast
{
    [[NewsBlast shareNews] cancelShowNewsBlast];
}

/* NewsBlastDelegate methods
 * In C# code, there should be some matched methods to them and named 'NBViewDidShow','NBViewDidNotShow',
 * 'NBViewDidClickCancel','NBViewDidClickOK'
 *
 */

#pragma mark - NewsBlastDelegate

//Invoked when newsblast did show.
- (void)viewDidShow:(NewsBlast *)newsBlast
{
    static NSString *methodName = @"NBViewDidShow";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

//Invoked when newsblast didn't show.
- (void)viewDidNotShow:(NewsBlast *)newsBlast
{
    static NSString *methodName = @"NBViewDidNotShow";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

//Invoked when user clicked cancel button on newsblast.
- (void)viewDidClickCancel:(NewsBlast *)newsBlast
{
    static NSString *methodName = @"NBViewDidClickCancel";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

//Invoked when user clicked ok button on newsblast.
- (void)viewDidClickOK:(NewsBlast *)newsBlast
{
    static NSString *methodName = @"NBViewDidClickOK";
    if([self.objectName length])
    {
        UnitySendMessage([self.objectName UTF8String],[methodName UTF8String],"");
    }
}

//To determine whether should display newblast.
- (BOOL)shouldDisplayNewsBlast
{
    return YES;
}

@end

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
    
    // To set the name of game object for UnitySendMessage method
    void _SetGameObjectNameForNB (char* gameObjectName)
    {
        [[SSCNewsBlastAdapter getInstance] setGameObjectName:gameObjectName];
    }
    
    // To start news
    void _StartNews (bool isDebugMode = false)
    {
        [[SSCNewsBlastAdapter getInstance] startNews:isDebugMode];
    }
    
    // To cancel show newsblast
    void _CancelShowNewsBlast()
    {
        [[SSCNewsBlastAdapter getInstance] cancelShowNewsBlast];
    }
}
